#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.8.1
 Author:         myName

 Script Function:
	Template AutoIt script.

#ce ----------------------------------------------------------------------------

Global $spot_cristal[15][7][4] = [ _
[[14, 17, 16777215, 0],[23, 18, 15284357, 14494582],[26, 24, 11830937, 12101301],[16, 27, 16777215, 16053503],[24, 30, 11554976, 0],[18, 33, 15685284, 0],[0, 0, 0, 0]], _
[[62, 48, 16764386, 0],[69, 53, 14697095, 0],[59, 58, 16766952, 0],[67, 59, 7490411, 0],[68, 67, 12630719, 12629947],[63, 69, 16618688, 16491463],[0, 0, 0, 0]], _
[[110, 94, 16765416, 0],[99, 97, 16772091, 0],[116, 99, 6567514, 7423063],[105, 103, 16318463, 16777215],[113, 109, 13923474, 16541606],[0, 0, 0, 0],[0, 0, 0, 0]], _
[[132, 30, 15038116, 15329769],[121, 34, 16473247, 0],[136, 37, 8855597, 0],[114, 40, 15110320, 0],[129, 42, 11746415, 16398220],[120, 44, 16762333, 0],[0, 0, 0, 0]], _
[[183, 114, 16559310, 0],[180, 121, 14074315, 14276312],[187, 122, 10891875, 5386814],[180, 128, 16114409, 16698336],[186, 131, 10568041, 7161703],[0, 0, 0, 0],[0, 0, 0, 0]], _
[[235, 117, 16602523, 16606118],[243, 121, 16777215, 15921919],[235, 124, 12152718, 11960494],[253, 124, 16737446, 0],[245, 133, 16777215, 8873831],[252, 137, 16745656, 0],[0, 0, 0, 0]], _
[[291, 110, 7212639, 10361940],[280, 112, 15972334, 16749786],[285, 120, 11292317, 11428226],[296, 120, 16659357, 0],[279, 122, 15000804, 13487565],[281, 129, 7230808, 7033688],[0, 0, 0, 0]], _
[[288, 69, 16697567, 0],[276, 72, 16767998, 0],[293, 73, 10986663, 0],[285, 74, 11503017, 11030389],[282, 84, 16757458, 16744373],[291, 84, 16754149, 16759021],[0, 0, 0, 0]], _
[[333, 106, 12340347, 11354011],[325, 107, 12087435, 15942541],[328, 116, 9720679, 9596276],[335, 117, 9913958, 7487083],[331, 122, 11815317, 11880854],[339, 123, 12366523, 0],[324, 125, 13466266, 0]], _
[[338, 64, 16774650, 0],[328, 67, 16730774, 16476068],[340, 70, 11434918, 0],[332, 73, 11304079, 11037599],[321, 76, 16739764, 16738226],[329, 79, 9246024, 5051718],[0, 0, 0, 0]], _
[[407, 83, 7625571, 9652868],[396, 94, 9587817, 10845582],[404, 94, 8412774, 10899092],[410, 95, 8930162, 0],[394, 101, 10311796, 9390467],[403, 101, 7557208, 8863353],[413, 103, 9786733, 0]], _
[[418, 26, 12857964, 0],[416, 35, 16464780, 15086977],[426, 40, 11549291, 7684950],[416, 45, 16759510, 0],[424, 49, 16764128, 0],[0, 0, 0, 0],[0, 0, 0, 0]], _
[[450, 39, 15168164, 0],[448, 46, 14663631, 0],[454, 49, 11441567, 8205682],[448, 55, 13404323, 15050946],[458, 57, 16760793, 16174557],[452, 59, 8409439, 14653108],[0, 0, 0, 0]], _
[[464, 7, 13270943, 16340121],[446, 10, 10894947, 0],[456, 13, 16747965, 0],[466, 15, 8018029, 12280713],[450, 17, 11546467, 0],[457, 20, 16752583, 16751044],[450, 26, 12479394, 0]], _
[[482, 2, 16676270, 14971809],[488, 2, 12073583, 8071788],[487, 7, 16761050, 16742834],[480, 9, 12019080, 12349865],[481, 20, 16039380, 0],[0, 0, 0, 0],[0, 0, 0, 0]] _
]
Global $limit_energy = 25
Global $limit_cristal = 25
Global $field[4] = [122,310,622,460]
;count_field(90,177)120, 310, 620, 460
;210 487 710 637
;208 487 708 637
Global $img_start = 10000
#include "points.au3"


Func count_field($l,$t,$r,$b)
   Local $hBMP, $length, $tBits,$w,$h
   $hBMP = _ScreenCapture_Capture("",$l,$t,$r,$b)
   
   $w = $r - $l + 1
   $h = $b - $t + 1
   $length = $w * $h
   $tBits = DllStructCreate('dword[' & $length & ']')
   _WinAPI_GetBitmapBits($hBMP, 4 * $length, DllStructGetPtr($tBits))

   Local $count = count_test($tBits,$w)
   
   _WinAPI_DeleteObject($hBMP)
   _WinAPI_DeleteObject($tBits)

   Return $count
EndFunc


Func count_quantity(ByRef $spot,$l,$t,$r,$b)
   Local $hBMP, $length, $tBits,$w,$h
   $hBMP = _ScreenCapture_Capture("",$l,$t,$r,$b)
   
   $img_start += 1
   Local $filename = "sc\img" & $img_start & ".bmp"
   _ScreenCapture_SaveImage($filename,$hBMP,False)
   _DebugOut($l & " " & $t & " " & $r & " " & $b & " image: " & $filename)

   $w = $r - $l + 1
   $h = $b - $t + 1
   $length = $w * $h
   $tBits = DllStructCreate('dword[' & $length & ']')
   _WinAPI_GetBitmapBits($hBMP, 4 * $length, DllStructGetPtr($tBits))

   reg_test($tBits,$w,$img_start)
   
   ;_DebugOut("start l=" & $l & " t=" & $t)
   Local $c = 0
   For $i = 0 To 14
	  Local $cp = 0
	  For $j = 0 To 6
		 Local $shift = $spot[$i][$j][0] + $spot[$i][$j][1] * $w + 1
		 Local $color = BitAND(DllStructGetData($tBits, 1, $shift),0xffffff)
		 If Not $color = 0 And ($color = $spot[$i][$j][2] Or $color = $spot[$i][$j][3]) Then
			;_DebugOut($i & " " & $j & " [" & $points[$j][0] & ", " & $points[$j][1] & "] " & " c=" & Hex($color,6) & " " & $shift)
			$cp += 1
		 EndIf
	  Next
	  
	  Local $add = 0
	  If $cp > 4 Then
		 $add = 3
	  ElseIf $cp > 1 Then
		 $add = 2
	  ElseIf $cp > 0 Then
		 $add = 1
	  EndIf
	  $c += $add
	  ;_DebugOut($i & " cp=" & $cp & " a=" & $add  & " c=" & $c)
   Next
   ;_DebugOut("done " & $c)

   _WinAPI_DeleteObject($hBMP)
   _WinAPI_DeleteObject($tBits)

   Return $c
EndFunc

Func field_durty($pos,$l,$t,$r,$b)
   Local $cs = PixelChecksum($l,$t,$r,$b)
   ;_DebugOut("(" & $l & " " & $t & " " & $r & " " & $b & ") cs=" & $cs & " old=" & $tmp_value[$pos])
   If $tmp_value[$pos] = $cs Then
	  Return False
   ElseIf $substep[$pos] > 50 Then
	  _DebugOut("calc on durty field")
	  Return False
   Else
	  $substep[$pos] += 1
	  $tmp_value[$pos] = $cs
	  Return True
   EndIf
EndFunc

Func count_common($pos,$index)
   ;_DebugOut("called count_common(" & $pos & "," & $index & ")")
   Local $p = $winlist[$pos]
   Local _
	  $l = $p[0] + $p[2] + $field[0], _
	  $t = $p[1] + $p[3] + $field[1], _
	  $r = $p[0] + $p[2] + $field[2], _
	  $b = $p[1] + $p[3] + $field[3]
   If field_durty($pos,$l+200,$t+60,$r-50,$b-20) Then Return -1
   ;_DebugOut("try count_field")
   Local $count = count_field($l,$t,$r,$b)
   _DebugOut("found: e=" & $count[0] & " c=" & $count[1])
   Return $count[$index]
EndFunc

Func count_cristal($pos)
   ;_DebugOut("called count_cristal(" & $pos & ")")
   Local $count = count_common($pos,1)
   If $count < 0 Then Return True
   If $count >= $limit_cristal Then
	  StopWork($pos,"cristal " & $count * 45)
   EndIf
   Return False
EndFunc

Func count_energy($pos)
   Local $count = count_common($pos,0)
   If $count < 0 Then Return True
   If $count >= $limit_energy Then
	  StopWork($pos,"energy " & $count * 2)
   EndIf
   Return False
EndFunc


Func count_cristal_old($pos)
   Local $p = $winlist[$pos]
   ;_DebugOut("f=" & $field[0] & ","  & $field[1] & "," & $field[2] & "," & $field[3] & " w=" & $p[0] & "," & $p[1] & " ic="  & $p[2] & "," & $p[3]) 
   Local _
	  $l = $p[0] + $p[2] + $field[0], _
	  $t = $p[1] + $p[3] + $field[1], _
	  $r = $p[0] + $p[2] + $field[2], _
	  $b = $p[1] + $p[3] + $field[3]
   If field_durty($pos,$l+200,$t+60,$r-50,$b-20) Then Return True
   Local $count = count_quantity($spot_cristal,$l,$t,$r,$b)
   _DebugOut("found: " & $count)
   ;If $count > $limit Then
	  StopWork($pos,"cristal " & $count * 45)
   ;EndIf
   Return False
EndFunc

Func search_item($pos)
   message($pos,"search_item")
   Return False
EndFunc

Func search_monster($pos)
   message($pos,"search_monster")
   Return False
EndFunc

